package CollegeInformation;

import javax.swing.*;
import java.util.List;

public class Service2 {

   static DataDao dao=new DataDao();
//   添加操作
    public static void addteacher(){
        String id;
        String no = JOptionPane.showInputDialog(View2.contentPane,
                "请选择你要添加的类型:1.教师 2.实验员 3.行政人员 4.教师兼行政人员"
                ,"输入",JOptionPane.QUESTION_MESSAGE);
        if("1".equals(no)){
            while (true) {
                 id = JOptionPane.showInputDialog(View2.contentPane,
                        "请输入教师的编号：", "输入", JOptionPane.QUESTION_MESSAGE);
                if (!Service2.searchId(id)) {
                    JOptionPane.showMessageDialog(View2.contentPane, "已有该教师,请重新输入ID!", "消息提示",
                            JOptionPane.INFORMATION_MESSAGE);
                    continue;
                }else{
                 break;
                }
            }
//            取消的BUG
            try {
                if(id.length()<=0){
                    return;
                }
            }catch(Exception e){
                e.printStackTrace();
            }

//            弹出增加信息的对话框
            addDialog dialog = new addDialog(id);
            dialog.pack();
            dialog.setVisible(true);
        if(!searchId(id)){
            //            弹出结果
            JOptionPane.showMessageDialog(View2.contentPane, "添加教师成功", "消息提示",
                    JOptionPane.INFORMATION_MESSAGE);
        }

        }
        if("2".equals(no)) {
            while (true) {
                id = JOptionPane.showInputDialog(View2.contentPane,
                        "请输入实验员的编号：：", "输入", JOptionPane.QUESTION_MESSAGE);
                if (!Service2.searchId(id)) {
                    JOptionPane.showMessageDialog(View2.contentPane, "已有该实验员,请重新输入ID!", "消息提示",
                            JOptionPane.INFORMATION_MESSAGE);
                    continue;
                } else {
                    break;
                }
            }
            addlamember dialog = new addlamember(id);
            dialog.pack();
            dialog.setVisible(true);
            if(!searchId(id)) {
                //            弹出结果
                JOptionPane.showMessageDialog(View2.contentPane, "添加实验员成功", "消息提示",
                        JOptionPane.INFORMATION_MESSAGE);
            }
        }
        if("3".equals(no)) {
            while (true) {
                id = JOptionPane.showInputDialog(View2.contentPane,
                        "请输入行政人员的编号：：", "输入", JOptionPane.QUESTION_MESSAGE);
                if (!Service2.searchId(id)) {
                    JOptionPane.showMessageDialog(View2.contentPane, "已有该行政人员,请重新输入ID!", "消息提示",
                            JOptionPane.INFORMATION_MESSAGE);
                    continue;
                } else {
                    break;
                }
            }
            addofficer dialog = new addofficer(id);
            dialog.pack();
            dialog.setVisible(true);
            if(!searchId(id)) {
                //            弹出结果
                JOptionPane.showMessageDialog(View2.contentPane, "添加行政人员成功", "消息提示",
                        JOptionPane.INFORMATION_MESSAGE);
            }
        }
        if("4".equals(no)) {
            while (true) {
                id = JOptionPane.showInputDialog(View2.contentPane,
                        "请输入教师兼行政人员的编号：：", "输入", JOptionPane.QUESTION_MESSAGE);
                if (!Service2.searchId(id)) {
                    JOptionPane.showMessageDialog(View2.contentPane, "已有该人员,请重新输入ID!", "消息提示",
                            JOptionPane.INFORMATION_MESSAGE);
                    continue;
                } else {
                    break;
                }
            }
            addTofficer dialog = new addTofficer(id);
            dialog.pack();
            dialog.setVisible(true);
            if(!searchId(id)) {
                //            弹出结果
                JOptionPane.showMessageDialog(View2.contentPane, "添加教师兼行政人员成功", "消息提示",
                        JOptionPane.INFORMATION_MESSAGE);
            }

        }
    }
    public static void addFunc1(String id,String name,String sex,int age,String department,String major,String title){
        //连接View类传来的数据和DAO类的add方法

        Teacher teacher=new Teacher(id,name,sex,age,"teacher",department,major,title);
        dao.add(teacher);
        System.out.println("添加教师成功");
    }
    public static void addFunc2(String id,String name,String sex,int age,String lab,String job){
        //连接View类传来的数据和DAO类的add方法
        System.out.println("添加实验员成功");
        LabMember labMember=new LabMember(id,name,sex,age,"labmember",lab,job);
        dao.add(labMember);
    }
    public static void addFunc3(String id,String name,String sex,int age,String political, String otitle){
        System.out.println("添加行政人员成功");
        Officer officer=new Officer(id,name,sex,age,"Officer",political,otitle);
        dao.add(officer);
    }
    public static void addFunc4(String id, String name, String sex, int age, String department, String major, String title, String TOpolitical){
        System.out.println("添加教师兼行政人员");
        TOfficer tOfficer=new TOfficer(id,name,sex,age,"TOfficer",department,major,title,TOpolitical);
        dao.add(tOfficer);
    }
//显示所有数据库的数据
    public static void displayallFunc(){
        List<Employee> employeeList= dao.displayall();
        String str = "";
        if(employeeList.size()==0){
            JOptionPane.showMessageDialog(View2.contentPane, "不好意思，目前没有学生信息可供查询，请回去重新输入你的选择", "消息提示",
                    JOptionPane.INFORMATION_MESSAGE);
        }else{
            for(Employee employee:employeeList){
                str += (employee.toString() + "\n");
            }
            View2.textArea.setText(str);
        }


//        dao.displayall();
    }
//统计人数
    public static void statistic(){

        statisticResult  str=dao.statistic();
        View2.textArea.setText(str.toString());
    }
//查询所有教师
    public static void search(){

        String id = JOptionPane.showInputDialog(View2.contentPane,
                "请输入你要查询教师的ID"
                ,"输入",JOptionPane.QUESTION_MESSAGE);
        if (Service2.searchId(id)) {//数据库中不存在该ID
            JOptionPane.showMessageDialog(View2.contentPane, "没有该教师,请重新输入ID!", "消息提示",
                    JOptionPane.INFORMATION_MESSAGE);
            return;
        } else {
            Employee employee = dao.search(id);
            showteacher dialog = new showteacher(employee);
            dialog.pack();
            dialog.setVisible(true);
        }
//        String age = JOptionPane.showInputDialog(View2.contentPane,
//                "请输入你要查询教师的年龄"
//                ,"输入",JOptionPane.QUESTION_MESSAGE);
//
//        if (Service2.searchage(age)) {//数据库中不存在该ID
//            JOptionPane.showMessageDialog(View2.contentPane, "没有该教师,请重新输入年龄!", "消息提示",
//                    JOptionPane.INFORMATION_MESSAGE);
//            return;
//        } else {
//            Employee employee = dao.search(id);
//            showteacher dialog = new showteacher(employee);
//            dialog.pack();
//            dialog.setVisible(true);
//        }



    }
//    删除教师操作
    public static void delete(){
        String id;

     id = JOptionPane.showInputDialog(View2.contentPane,
            "请输入要删除教师的ID：", "输入", JOptionPane.QUESTION_MESSAGE);
     try {
         if (Service2.searchId(id) && id.length() > 0) {//数据库中不存在该ID
             JOptionPane.showMessageDialog(View2.contentPane, "没有该教师,请重新操作!", "消息提示",
                     JOptionPane.INFORMATION_MESSAGE);
             return;
         } else {
             if (dao.delete(id))
                 JOptionPane.showMessageDialog(View2.contentPane, "删除成功", "消息提示",
                         JOptionPane.INFORMATION_MESSAGE);
             else
                 JOptionPane.showMessageDialog(View2.contentPane, "删除失败", "消息提示",
                         JOptionPane.INFORMATION_MESSAGE);
         }
     }catch (Exception e){
         e.printStackTrace();
     }

}
//通过ID判断教师是否存在
    public static boolean searchId(String id){
//
        if( dao.searchByid(id))//在数据库找不到符合该ID的教师，返回真
            return true;//不存在该ID的教师
        else
            return false;//存在该ID的教师

    }

//    修改教师
    public static void modify(Employee employee){
        dao.modify(employee);
        System.out.println("修改成功！");
    }
//    修改数据页面
    public static void showchange(Employee employee){
        updatateacher dialog = new updatateacher(employee);
        dialog.pack();
        dialog.setVisible(true);
    }
}
